package org.example.day20240828.homeWork;

import java.util.*;

import java.util.Objects;

class Student {
    private String name;
    private String sex;

    public Student(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student)) return false;
        Student student = (Student) o;
        return Objects.equals(getName(), student.getName()) && Objects.equals(getSex(), student.getSex());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getSex());
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        List<Student> classes = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String[] strs = scanner.nextLine().split(" ");
            classes.add(new Student(strs[1], strs[0]));
        }
        for (int i = 0; i < classes.size(); i++) {
            for (int j = classes.size() - 1; j > 0; j--) {
                if(!classes.get(i).getSex().equals(classes.get(j).getSex()) && !classes.get(j).getSex().equals("-1") && !classes.get(i).getSex().equals("-1")){
                    System.out.println(classes.get(i).getName() + " " + classes.get(j).getName());
                    classes.get(i).setSex("-1");
                    classes.get(j).setSex("-1");
                }
            }
        }
    }
}
